package com.mes.service.manu;

import com.mes.common.MesPdaConstant;
import com.yy.base.common.dao.Dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class fzJobWriteInRfidListService {
    @Autowired
    private Dao dao;

    //加载飞织作业信息
    public List<Map<String,Object>> selectFlyWeavList(Map map) {
        String sql ="select o.emeso_job_order_id id, o.code, p.name, o.material_ok, " +
                "   f.name fzj, to_char(oe.exe_date_from,'yyyy-mm-dd hh24:mm:ss') as exe_date_from, " +
                "   oe.exe_date_to, oe.status, mes_get_xm_color(o.emeso_manuorder_id) colorname, " +
                "       (select mo.foot_size_code from emeso_manuorder mo where mo.emeso_manuorder_id = o.emeso_manuorder_id) size_code " +
                "  from emes_process pr, eam_facilities f, m_product p, emeso_job_order o, Emeso_Job_Order_Exe oe\n" +
                " where p.m_product_id = o.m_product_id\n" +
                "   and f.eam_facilities_id = oe.eam_facilities_id\n" +
                "\t and pr.emes_process_id = oe.emes_process_id\n" +
                "   and oe.emeso_job_order_id = o.emeso_job_order_id\n" +
                "   and pr.Route_No = " + MesPdaConstant.FLYWEAVCODE;

        if(map.get("status") != null && "Y".equals(map.get("status").toString())) {
            sql += "    and nvl(o.material_ok, '') = 'Y'";
        } else {
            sql += "    and nvl(o.material_ok, '') <> 'Y'";
        }
        if(map.get("flyWeav")!= null && !"".equals(map.get("flyWeav"))){
            sql += "    and oe.Eam_Facilities_ID = '"+map.get("flyWeav")+"'";
        }
        if(map.get("jobCode")!= null && !"".equals(map.get("jobCode"))){
            sql += "    and o.Code = '"+map.get("jobCode")+"'";
        }
        return dao.query(sql);
    }

    public List<Map<String,Object>> selectFlyWeavs() {
        return dao.query("SELECT " +
                "       a.Eam_Facilities_Id eamFacilitiesId,a.name , a.Position " +
                "  from Eam_Facilities a " +
                " where exists(select 1 from Emes_Process_Facilities b,Emes_Process c " +
                "               where b.Emes_Process_Id = c.emes_process_id  " +
                "                 and COALESCE(c.code,to_char(c.route_no))='" + MesPdaConstant.FLYWEAVCODE+"'"+
                "                 and b.Eam_Facilities_Id = a.Eam_Facilities_Id)");
    }

    public List<Map<String,Object>> selectJobStatus() {
        return dao.query("SELECT " +
                "   t.tablename, c.columnname, rl.value, rl.name, rl.seqno from ad_ref_list rl, ad_column c, ad_table t " +
                "  where rl.ad_reference_id = c.ad_reference_value_id " +
                "   and c.ad_table_id = t.ad_table_id " +
                "   and upper(t.tablename) = upper('Emeso_Job_Order_Exe') " +
                "   and upper(c.columnname) = upper('Status')" +
                "   order by t.tablename, c.columnname, rl.seqno");
    }

    //将作业工单的辅料写入RFID设为已绑定
    @SuppressWarnings("unchecked")
    public boolean UpdateJobMaterialok(String code) {
        String sql =  " update Emeso_Job_Order  set material_ok = 'Y' where  code = ? ";
        return dao.executeSql(sql, code) > 0;
    }
}
